package com.zrc.japktool.util;

import java.io.*;

/**
 * ADB安装工具类 - 一行代码实现APK安装，使用TaskLog接口输出日志
 */
public class AdbUtil {

    /**
     * 一行代码安装APK
     * @param apkFile APK文件
     * @param log 日志输出接口
     */
    public static void installApk(File apkFile, TaskLog log) {
        try {
            log.tip("apk安装: 安装开始" + apkFile.getName());
//            if (CmdUtil.isWinOS()){
//                if (!FileUtil.isExistInWorkDir("adb.exe","AdbWinApi.dll","AdbWinUsbApi.dll")) {
//                    log.info("缺少adb环境,即将构建...");
//                    FileUtil.copyResourceFile2WorkDir("windows/adb.exe", "windows/AdbWinApi.dll", "windows/AdbWinUsbApi.dll");
//                }
//            }else if (CmdUtil.isMacOS()){
//                log.error("未适配mac系统...");
//            }else {
//                log.error("未适酌linux系统...");
//            }
            log.info("adb环境检测结束...");
            String workingDir = System.getProperty("user.dir").replace("\\", "/");
            String adbPath = workingDir + "/app/adb/adb.exe";
            
            // Check if adb exists
            File adbFile = new File(adbPath);
            if (!adbFile.exists()) {
                log.error("adb工具不存在: " + adbPath);
                throw new RuntimeException("adb工具不存在: " + adbPath);
            }
            
            // Check if APK file exists
            if (!apkFile.exists()) {
                log.error("APK文件不存在: " + apkFile.getAbsolutePath());
                throw new RuntimeException("APK文件不存在: " + apkFile.getAbsolutePath());
            }
            
            String cmd = adbPath + " install -t \"" + apkFile.getAbsolutePath().replace("\\", "/") + "\"";
            CmdUtil.exec(cmd, log);
            log.tip("apk安装: 安装成功");
        } catch (Exception e) {
            log.error("apk安装: 安装失败:" + e.getMessage());
            throw new RuntimeException(e);
        }
    }

}
    